package com.ruoyi.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.business.entity.YytTeamAssociationUser;
import com.ruoyi.common.business.service.YytMemberService;
import com.ruoyi.common.business.service.YytTeamAssociationService;
import com.ruoyi.common.business.service.YytTeamAssociationUserService;
import com.ruoyi.utils.RedisService;
import com.ruoyi.utils.ResponseResult;
import com.ruoyi.vo.AddTeamInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MrPing
 * @since 2022-09-05
 */
@RestController
@RequestMapping("/association")
public class YytTeamAssociationUserController {

    @Autowired
    private YytTeamAssociationUserService yytTeamAssociationUserService;

    @Autowired
    private YytTeamAssociationService yytTeamAssociationService;

    @Autowired
    private YytMemberService yytMemberService;

    @Autowired
    private RedisService redisService;



    @GetMapping("/getUserIsSign")
    public ResponseResult getUserIsSign(@RequestParam("openId") String openId) {
        QueryWrapper<YytMember> wrapper = new QueryWrapper<>();
        wrapper.eq("wechat_id", openId).eq("del_flag", 0).eq("status", 0);
        return ResponseResult.success(yytMemberService.list(wrapper));
    }


    @PostMapping("/addUserByTeam")
    public ResponseResult addUserByTeam(@RequestBody AddTeamInfoVo addTeamInfoVo) {
        if(StringUtils.isEmpty(addTeamInfoVo.getCodeKey())){
            return ResponseResult.failure("请输入验证码!");
        }
        Object code = redisService.get(addTeamInfoVo.getCodeKey());
        if(code==null){
            return ResponseResult.failure("验证码已过期!");
        }
        String s = String.valueOf(code);
        if(!s.equals(addTeamInfoVo.getCode())){
            return ResponseResult.failure("验证码不正确!");
        }
        YytMember yytMember = new YytMember();
        yytMember.setId(addTeamInfoVo.getMemberId());
        yytMember.setCardId(addTeamInfoVo.getCardId());
        yytMember.setPhone(addTeamInfoVo.getPhone());
        yytMemberService.updateById(yytMember);

        YytTeamAssociationUser associationUser = new YytTeamAssociationUser();
        associationUser.setTeamId(addTeamInfoVo.getTeamId());
        associationUser.setMemberId(addTeamInfoVo.getMemberId());
        return ResponseResult.success(yytTeamAssociationUserService.save(associationUser));
    }


    @GetMapping("/getUserIsTeam")
    public ResponseResult getUserIsTeam() {
        return ResponseResult.success(yytTeamAssociationService.list());
    }

}
